package com.study.suanfa;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @description: SubStringTest  字符串分割，来自高手的五到算法面试题
 * @date: 2020/11/6 17:22
 * @author: hcm
 * @version: 1.0
 */
public class SubStringTest {
    public static void main(String[] args) {

        String data = "aa&&bb&&&&cc&&&&&&&&&&kk";
        String addData = "";
        List<String> l = new ArrayList<String>();
        //给定一个判断类型，判断是否连续的分隔符
        Boolean flag = false;
        for (int i = 0; i < data.length(); i++) {
            //遍历拿到每一个字符
            char c = data.charAt(i);

            //System.out.println(c);
            //如果当前的拿到的是分割符，并且进入了分隔符连环段
            if('&' == c && flag == false ){
                //表示进入分隔符连环段
                flag = true;
                l.add(addData);
                addData ="";
                //跳过当前循环
                continue;
            }
            //跳出分隔符连环段的条件
            if('&'!=c){
                flag = false;
            }
            //分隔符连环段不追加，跳过
            if(flag ==true){
                continue;
            }
                addData += c;
            //触碰到底部
            if(i == data.length()-1){
                l.add(addData);
            }

        }
        System.out.println(l);
        //把数组重新组合在一起，用&&拼接
        StringBuilder s = new StringBuilder();
        AtomicInteger num = new AtomicInteger();
        AtomicInteger len = new AtomicInteger();
        len.set(l.size());
        System.out.println(num.get());
        l.forEach(i-> {
           if(num.get() == len.get()){
               s.append(i);
           }else{
               s.append(i+"&&");
           }

        });
        System.out.println("复原->"+s);
    }
}
